var app = new Vue({
    el: '#orderList',
    data () {
        return {
            types: [{
                value: "ALL",
                label: '全部'
            }, {
                value: "WAIT_QUERY",
                label: '待查询'
            }, {
                value: "IN_PROGRESS",
                label: '进行中'
            }, {
                value: "FINISHED",
                label: '已完成'
            }, {
                value: "CANCELED",
                label: '已取消'
            }],
            type: '',
            loop: false,
            autoPlay: false,
            showDots: false,
            slideOptions: {
                click: false,
                listenScroll: true,
                probeType: 3,
                directionLockThreshold: 0
            },
            currentPage: 1,
            totalPage: 2,
            pageSize: 8,
            pullUpLoad: false,
            pullUpLoadThreshold: 0,
            pullUpLoadNoMoreTxt: '没有更多了！',
            allOrderListData: null,
            queryOrderListData: null,
            inProgressOrderListData: null,
            finishedOrderListData: null,
            canceledOrderListData: null,
            typeTitle: '',
            typeName: '',
        }
    },
    computed: {
        initialIndex () {
            let index = 0
            index = this.findIndex(this.types, item => item.label === this.type)
            return index
        },
        scrollOptions () {
            return {
                pullUpLoad: this.pullUpLoadObj
            }
        },
        pullUpLoadObj: function() {
            return this.pullUpLoad ? {
                threshold: parseInt(this.pullUpLoadThreshold),
                txt: {
                    noMore: this.pullUpLoadNoMoreTxt
                }
            } : false
        }
    },
    watch: {
        type (val) {
            switch (val) {
                case '全部':
                this.typeName = ''
                this.typeTitle = '全部订单'
                break
                case '待查询':
                this.typeName = '待查询的'
                this.typeTitle = '待查询订单'
                break
                case '进行中':
                this.typeName = '进行中的'
                this.typeTitle = '进行中订单'
                break
                case '已完成':
                this.typeName = '已完成的'
                this.typeTitle = '已完成订单'
                break
                case '已取消':
                this.typeName = '已取消的'
                this.typeTitle = '已取消订单'
                break
            }
        }
    },
    created() {
        let type = getUrlParams().type
        if (type) {
            this.type = decodeURI(type)
            switch (this.type) {
                case '待查询':
                    this.getQueryOrderListData()
                    break
                case '进行中':
                    this.getInProgressOrderListData()
                    break
                case '进行中':
                    this.getFinishedOrderListData()
                    break
                case '已取消':
                    this.getCanceledOrderListData()
                    break
            }
        } else {
            this.type = '全部'
            this.getAllOrderListData()
        }
    },
    methods: {
        findIndex(ary, fn) {
            if (ary.findIndex) {
                return ary.findIndex(fn)
            }
            let index = -1
            ary.some(function (item, i, ary) {
                const ret = fn.call(this, item, i, ary)
                if (ret) {
                    index = i
                    return ret
                }
            })
            return index
        },
        // 返回
        goback () {
            window.location.href = "../my/my.html"
        },
        clickHandler (label) {
            this.type = label
        },
        changePage (index) {
            this.currentPage = 1
            this.pullUpLoad = false
            this.type = this.types[index].label
            if (index === 0) {
                this.$refs.allOrderList.scrollTo(0, 0, 200, 'linear')
                this.getAllOrderListData()
            } else if (index === 1) {
                this.$refs.queryOrderList.scrollTo(0, 0, 200, 'linear')
                this.getQueryOrderListData()
            } else if (index === 2) {
                this.$refs.inProgressOrderList.scrollTo(0, 0, 200, 'linear')
                this.getInProgressOrderListData()
            } else if (index === 3) {
                this.$refs.finishedOrderList.scrollTo(0, 0, 200, 'linear')
                this.getFinishedOrderListData()
            } else if (index === 4) {
                this.$refs.canceledOrderList.scrollTo(0, 0, 200, 'linear')
                this.getCanceledOrderListData()
            }
        },
        allOrderListMore () {
            this.currentPage ++
            if(this.currentPage > this.totalPage){
                this.$refs.allOrderList.forceUpdate()
                return
            }
            this.getAllOrderListData()
        },
        getAllOrderListData () {
            Axios.post('/goods/order/getOrderList.do',{
                userStatus: 'ALL',
                currentPage: this.currentPage,
                pageSize: this.pageSize
            }).then(res => {
                if (res.respCode === 'S') {
                    let list = res.data.data
                    list.forEach(item => {
                        switch (item.status) {
                            case 'PUBLISHING_PROCUREMENT':
                            item.status = '正在发布采买任务'
                            break
                            case 'PROCURING':
                            item.status = '商品采买中'
                            break
                            case 'PROCURE_FINISHED':
                            item.status = '采买完成'
                            break
                            case 'DELIVERING':
                            item.status = '配送中'
                            break
                            case 'DELIVER_FINISHED':
                            item.status = '已完成'
                            break
                            case 'FINISHED':
                            item.status = '已完成'
                            break
                            case 'CANCELED':
                            item.status = '已取消'
                            break
                        }
                        item.shoppingCarList.forEach(goods => {
                            switch (goods.status) {
                                case 'PROCURING':
                                goods.status = '采买中'
                                break
                                case 'LACK':
                                goods.status = '缺货'
                                break
                                case 'PRICE_HIGH':
                                goods.status = '价高未买'
                                break
                                case 'BOUGHT':
                                goods.status = '已买'
                                break
                                case 'WAIT_DEAL':
                                goods.status = '待采买'
                                break
                                case 'CANCELED':
                                goods.status = '已取消'
                                break
                            }
                        })
                    })
                    if (this.currentPage === 1) {
                        this.allOrderListData = list
                    } else {
                        this.allOrderListData = this.allOrderListData.concat(list)
                    }
                    this.totalPage = res.data.totalPage
                    if (this.currentPage < this.totalPage) {
                        this.pullUpLoad = true
                    }
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    this.$refs.allOrderList.forceUpdate()
                }
            })
        },
        queryOrderListMore () {
            this.currentPage ++
            if(this.currentPage > this.totalPage){
                this.$refs.queryOrderList.forceUpdate()
                return
            }
            this.getQueryOrderListData()
        },
        getQueryOrderListData () {
            Axios.post('/self/order/selfOrder.do',{
                currentPage: this.currentPage,
                pageSize: this.pageSize
            }).then(res => {
                if (res.respCode === 'S') {
                    let list = res.data.data
                    list.forEach(item => {
                        item.createTime = new Date(item.createTime).Format('yy/MM/dd')
                        if (item.goodsSkuList) {
                            item.goodsSkuList.forEach(goods => {
                                goods.minPrice = goods.minPrice / 100
                                goods.maxPrice = goods.maxPrice / 100
                            })
                        }
                    })
                    if (this.currentPage === 1) {
                        this.queryOrderListData = list
                    } else {
                        this.queryOrderListData = this.queryOrderListData.concat(list)
                    }
                    this.totalPage = res.data.totalPage
                    if (this.currentPage < this.totalPage) {
                        this.pullUpLoad = true
                    }
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    this.$refs.queryOrderList.forceUpdate()
                }
            })
        },
        inProgressOrderListMore () {
            this.currentPage ++
            if(this.currentPage > this.totalPage){
                this.$refs.inProgressOrderList.forceUpdate()
                return
            }
            this.getInProgressOrderListData()
        },
        getInProgressOrderListData () {
            Axios.post('/goods/order/getOrderList.do',{
                userStatus: 'IN_PROGRESS',
                currentPage: this.currentPage,
                pageSize: this.pageSize
            }).then(res => {
                if (res.respCode === 'S') {
                    let list = res.data.data
                    list.forEach(item => {
                        switch (item.status) {
                            case 'PUBLISHING_PROCUREMENT':
                            item.status = '正在发布采买任务'
                            break
                            case 'PROCURING':
                            item.status = '商品采买中'
                            break
                            case 'PROCURE_FINISHED':
                            item.status = '采买完成'
                            break
                            case 'DELIVERING':
                            item.status = '配送中'
                            break
                            case 'DELIVER_FINISHED':
                            item.status = '已完成'
                            break
                            case 'FINISHED':
                            item.status = '已完成'
                            break
                            case 'CANCELED':
                            item.status = '已取消'
                            break
                        }
                        item.shoppingCarList.forEach(goods => {
                            switch (goods.status) {
                                case 'PROCURING':
                                goods.status = '采买中'
                                break
                                case 'LACK':
                                goods.status = '缺货'
                                break
                                case 'PRICE_HIGH':
                                goods.status = '价高未买'
                                break
                                case 'BOUGHT':
                                goods.status = '已买'
                                break
                                case 'WAIT_DEAL':
                                goods.status = '待采买'
                                break
                                case 'CANCELED':
                                goods.status = '已取消'
                                break
                            }
                        })
                    })
                    if (this.currentPage === 1) {
                        this.inProgressOrderListData = list
                    } else {
                        this.inProgressOrderListData = this.inProgressOrderListData.concat(list)
                    }
                    this.totalPage = res.data.totalPage
                    if (this.currentPage < this.totalPage) {
                        this.pullUpLoad = true
                    }
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    this.$refs.inProgressOrderList.forceUpdate()
                }
            })
        },
        finishedOrderListMore () {
            this.currentPage ++
            if(this.currentPage > this.totalPage){
                this.$refs.finishedOrderList.forceUpdate()
                return
            }
            this.getFinishedOrderListData()
        },
        getFinishedOrderListData () {
            Axios.post('/goods/order/getOrderList.do',{
                userStatus: 'FINISHED',
                currentPage: this.currentPage,
                pageSize: this.pageSize
            }).then(res => {
                if (res.respCode === 'S') {
                    let list = res.data.data
                    list.forEach(item => {
                        switch (item.status) {
                            case 'PUBLISHING_PROCUREMENT':
                            item.status = '正在发布采买任务'
                            break
                            case 'PROCURING':
                            item.status = '商品采买中'
                            break
                            case 'PROCURE_FINISHED':
                            item.status = '采买完成'
                            break
                            case 'DELIVERING':
                            item.status = '配送中'
                            break
                            case 'DELIVER_FINISHED':
                            item.status = '已完成'
                            break
                            case 'FINISHED':
                            item.status = '已完成'
                            break
                            case 'CANCELED':
                            item.status = '已取消'
                            break
                        }
                        item.shoppingCarList.forEach(goods => {
                            switch (goods.status) {
                                case 'PROCURING':
                                goods.status = '采买中'
                                break
                                case 'LACK':
                                goods.status = '缺货'
                                break
                                case 'PRICE_HIGH':
                                goods.status = '价高未买'
                                break
                                case 'BOUGHT':
                                goods.status = '已买'
                                break
                                case 'WAIT_DEAL':
                                goods.status = '待采买'
                                break
                                case 'CANCELED':
                                goods.status = '已取消'
                                break
                            }
                        })
                    })
                    if (this.currentPage === 1) {
                        this.finishedOrderListData = list
                    } else {
                        this.finishedOrderListData = this.finishedOrderListData.concat(list)
                    }
                    this.totalPage = res.data.totalPage
                    if (this.currentPage < this.totalPage) {
                        this.pullUpLoad = true
                    }
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    this.$refs.finishedOrderList.forceUpdate()
                }
            })
        },
        canceledOrderListMore () {
            this.currentPage ++
            if(this.currentPage > this.totalPage){
                this.$refs.canceledOrderList.forceUpdate()
                return
            }
            this.getCanceledOrderListData()
        },
        getCanceledOrderListData () {
            Axios.post('/goods/order/getOrderList.do',{
                userStatus: 'CANCELED',
                currentPage: this.currentPage,
                pageSize: this.pageSize
            }).then(res => {
                if (res.respCode === 'S') {
                    let list = res.data.data
                    list.forEach(item => {
                        switch (item.status) {
                            case 'PUBLISHING_PROCUREMENT':
                            item.status = '正在发布采买任务'
                            break
                            case 'PROCURING':
                            item.status = '商品采买中'
                            break
                            case 'PROCURE_FINISHED':
                            item.status = '采买完成'
                            break
                            case 'DELIVERING':
                            item.status = '配送中'
                            break
                            case 'DELIVER_FINISHED':
                            item.status = '已完成'
                            break
                            case 'FINISHED':
                            item.status = '已完成'
                            break
                            case 'CANCELED':
                            item.status = '已取消'
                            break
                        }
                        item.shoppingCarList.forEach(goods => {
                            switch (goods.status) {
                                case 'PROCURING':
                                goods.status = '采买中'
                                break
                                case 'LACK':
                                goods.status = '缺货'
                                break
                                case 'PRICE_HIGH':
                                goods.status = '价高未买'
                                break
                                case 'BOUGHT':
                                goods.status = '已买'
                                break
                                case 'WAIT_DEAL':
                                goods.status = '待采买'
                                break
                                case 'CANCELED':
                                goods.status = '已取消'
                                break
                            }
                        })
                    })
                    if (this.currentPage === 1) {
                        this.canceledOrderListData = list
                    } else {
                        this.canceledOrderListData = this.canceledOrderListData.concat(list)
                    }
                    this.totalPage = res.data.totalPage
                    if (this.currentPage < this.totalPage) {
                        this.pullUpLoad = true
                    }
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    this.$refs.canceledOrderList.forceUpdate()
                }
            })
        },
        btnConfirmlOrder (item, index) {
            this.$createDialog({
                type: 'confirm',
                title: '温馨提示',
                content: '您确定要取消订单吗?',
                confirmBtn: {
                    text: '确定',
                    active: true,
                    disabled: false,
                    href: 'javascript:;'
                },
                cancelBtn: {
                    text: '取消',
                    active: false,
                    disabled: false,
                    href: 'javascript:;'
                },
                onConfirm: () => {
                    Axios.post('/goods/order/updateOrderStatus.do',{
                        ordersId: item.ordersId,
                        userStatus: 'FINISHED'
                    }).then(res => {
                        if (res.respCode === 'S') {
                            if (this.type === '全部') {
                                this.allOrderListData[index].status = '已完成'
                            } else if (this.type === '进行中') {
                                this.inProgressOrderListData[index].status = '已完成'
                            } else if (this.type === '已完成') {
                                this.finishedOrderListData[index].status = '已完成'
                            } else if (this.type === '已取消') {
                                this.canceledOrderListData[index].status = '已完成'
                            }
                        } else {
                            this.toast = this.$createToast({
                                txt: res.errorMsg,
                                type: 'txt'
                            })
                            this.toast.show()
                        }
                    })
                },
                onCancel: () => {}
            }).show()
        },
        btnBuy (item) {
            Axios.post('goods/order/againOrders.do',{
                ordersId: item.ordersId
            }).then(res => {
                if (res.respCode === 'S') {
                    if (res.data === 'success') {
                        this.toast = this.$createToast({
                            txt: '商品已加入购物车<br>请到购物车结算！',
                            type: 'txt'
                        })
                        this.toast.show()
                    } else if (res.data === 'no_price')  {
                        this.toast = this.$createToast({
                            txt: '该商品已下架，暂无价格',
                            type: 'txt'
                        })
                        this.toast.show()
                    }          
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                }
            })
        },
        toDetail (item) {
            window.location.href = 'member_order_detail.html?id=' + item.ordersId
        }
    }
})